package org.speedy;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class SpeedyConfig {

	//TODO
	//this class should be able to accept following command line
	// java -jar speedy.jar -cp xxx:xxx:xxx --verbose --test-filter "Test$" --package-filter "^com/gu"

	private final boolean verbose;
	private final String packageFilter;
	private final String testFilter;
	private List<String> changedClasses;

	@SuppressWarnings("unchecked")
	public SpeedyConfig(String ... commandLineArgs) {
		List<String> args = new LinkedList(Arrays.asList(commandLineArgs));

		verbose = extractVerbose(args);
		packageFilter = extractPackageFilter(args);
		testFilter = extractTestFilter(args);

		changedClasses = args;
	}

	private String extractTestFilter(List<String> args) {
		int position = args.indexOf("--test-filter");
		if (position == -1) {
			return ".+Test$";
		}
		args.remove(position);
		return args.remove(position);
	}

	private String extractPackageFilter(List<String> args) {
		int position = args.indexOf("--package-filter");
		if (position == -1) {
			return "";
			//TODO ignore for now - should throw meaningful exception
		}
		args.remove(position);
		return args.remove(position);
	}

	private boolean extractVerbose(List<String> args) {
		return args.remove("--verbose");
	}

	public List<String> getChangedClasses() {
		return changedClasses;
	}

	public boolean isVerbose() {
		return verbose;
	}

	public String getTestFilter() {
		return testFilter;
	}

	public String getPackageFilter() {
		return packageFilter;
	}
}